home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / BaseClasses / wxdebug.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  43.3 KB  |  1,311 lines

  1. //------------------------------------------------------------------------------
  2. // File: WXDebug.cpp
  3. //
  4. // Desc: DirectShow base classes - implements ActiveX system debugging
  5. //       facilities.    
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10.  
  11. #define _WINDLL
  12.  
  13. #include <streams.h>
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16.  
  17. #ifdef DEBUG
  18. #ifdef UNICODE
  19. #ifndef _UNICODE
  20. #define _UNICODE
  21. #endif // _UNICODE
  22. #endif // UNICODE
  23. #endif // DEBUG
  24.  
  25. #include <tchar.h>
  26.  
  27. #ifdef DEBUG
  28.  
  29. // The Win32 wsprintf() function writes a maximum of 1024 characters to it's output buffer.
  30. // See the documentation for wsprintf()'s lpOut parameter for more information.
  31. const INT iDEBUGINFO = 1024;                 // Used to format strings
  32.  
  33. /* For every module and executable we store a debugging level for each of
  34.    the five categories (eg LOG_ERROR and LOG_TIMING). This makes it easy
  35.    to isolate and debug individual modules without seeing everybody elses
  36.    spurious debug output. The keys are stored in the registry under the
  37.    HKEY_LOCAL_MACHINE\SOFTWARE\Debug\<Module Name>\<KeyName> key values
  38.    NOTE these must be in the same order as their enumeration definition */
  39.  
  40. TCHAR *pKeyNames[] = {
  41.     TEXT("TIMING"),      // Timing and performance measurements
  42.     TEXT("TRACE"),       // General step point call tracing
  43.     TEXT("MEMORY"),      // Memory and object allocation/destruction
  44.     TEXT("LOCKING"),     // Locking/unlocking of critical sections
  45.     TEXT("ERROR"),       // Debug error notification
  46.     TEXT("CUSTOM1"),
  47.     TEXT("CUSTOM2"),
  48.     TEXT("CUSTOM3"),
  49.     TEXT("CUSTOM4"),
  50.     TEXT("CUSTOM5")
  51.     };
  52.  
  53. const TCHAR CAutoTrace::_szEntering[] = TEXT("Entering: %s");
  54. const TCHAR CAutoTrace::_szLeaving[]  = TEXT("Leaving: %s");
  55.  
  56. const INT iMAXLEVELS = NUMELMS(pKeyNames);  // Maximum debug categories
  57.  
  58. HINSTANCE m_hInst;                          // Module instance handle
  59. TCHAR m_ModuleName[iDEBUGINFO];             // Cut down module name
  60. DWORD m_Levels[iMAXLEVELS];                 // Debug level per category
  61. CRITICAL_SECTION m_CSDebug;                 // Controls access to list
  62. DWORD m_dwNextCookie;                       // Next active object ID
  63. ObjectDesc *pListHead = NULL;               // First active object
  64. DWORD m_dwObjectCount;                      // Active object count
  65. BOOL m_bInit = FALSE;                       // Have we been initialised
  66. HANDLE m_hOutput = INVALID_HANDLE_VALUE;    // Optional output written here
  67. DWORD dwWaitTimeout = INFINITE;             // Default timeout value
  68. DWORD dwTimeOffset;             // Time of first DbgLog call
  69. bool g_fUseKASSERT = false;                 // don't create messagebox
  70. bool g_fDbgInDllEntryPoint = false;
  71. bool g_fAutoRefreshLevels = false;
  72.  
  73. const TCHAR *pBaseKey = TEXT("SOFTWARE\\Debug");
  74. const TCHAR *pGlobalKey = TEXT("GLOBAL");
  75. static CHAR *pUnknownName = "UNKNOWN";
  76.  
  77. TCHAR *TimeoutName = TEXT("TIMEOUT");
  78.  
  79. /* This sets the instance handle that the debug library uses to find
  80.    the module's file name from the Win32 GetModuleFileName function */
  81.  
  82. void WINAPI DbgInitialise(HINSTANCE hInst) {
  83.     InitializeCriticalSection(&m_CSDebug);
  84.     m_bInit = TRUE;
  85.  
  86.     m_hInst = hInst;
  87.     DbgInitModuleName();
  88.     if(GetProfileInt(m_ModuleName, TEXT("BreakOnLoad"), 0))
  89.         DebugBreak();
  90.     DbgInitModuleSettings(false);
  91.     DbgInitGlobalSettings(true);
  92.     dwTimeOffset = timeGetTime();
  93. }
  94.  
  95.  
  96. /* This is called to clear up any resources the debug library uses - at the
  97.    moment we delete our critical section and the object list. The values we
  98.    retrieve from the registry are all done during initialisation but we don't
  99.    go looking for update notifications while we are running, if the values
  100.    are changed then the application has to be restarted to pick them up */
  101.  
  102. void WINAPI DbgTerminate() {
  103.     if(m_hOutput != INVALID_HANDLE_VALUE) {
  104.         EXECUTE_ASSERT(CloseHandle(m_hOutput));
  105.         m_hOutput = INVALID_HANDLE_VALUE;
  106.     }
  107.     DeleteCriticalSection(&m_CSDebug);
  108.     m_bInit = FALSE;
  109. }
  110.  
  111.  
  112. /* This is called by DbgInitLogLevels to read the debug settings
  113.    for each logging category for this module from the registry */
  114.  
  115. void WINAPI DbgInitKeyLevels(HKEY hKey, bool fTakeMax) {
  116.     LONG lReturn;               // Create key return value
  117.     LONG lKeyPos;               // Current key category
  118.     DWORD dwKeySize;            // Size of the key value
  119.     DWORD dwKeyType;            // Receives it's type
  120.     DWORD dwKeyValue;           // This fields value
  121.  
  122.     /* Try and read a value for each key position in turn */
  123.     for(lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  124.  
  125.         dwKeySize = sizeof(DWORD);
  126.         lReturn = RegQueryValueEx(hKey,                       // Handle to an open key
  127.             pKeyNames[lKeyPos],         // Subkey name derivation
  128.             NULL,                       // Reserved field
  129.             &dwKeyType,                 // Returns the field type
  130.             (LPBYTE) &dwKeyValue,       // Returns the field's value
  131.             &dwKeySize);               // Number of bytes transferred
  132.  
  133.         /* If either the key was not available or it was not a DWORD value
  134.         then we ensure only the high priority debug logging is output
  135.         but we try and update the field to a zero filled DWORD value */
  136.  
  137.         if(lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD) {
  138.  
  139.             dwKeyValue = 0;
  140.             lReturn = RegSetValueEx(hKey,                   // Handle of an open key
  141.                 pKeyNames[lKeyPos],     // Address of subkey name
  142.                 (DWORD) 0,              // Reserved field
  143.                 REG_DWORD,              // Type of the key field
  144.                 (PBYTE) &dwKeyValue,    // Value for the field
  145.                 sizeof(DWORD));         // Size of the field buffer
  146.  
  147.             if(lReturn != ERROR_SUCCESS) {
  148.                 DbgLog((LOG_ERROR,0,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos]));
  149.                 dwKeyValue = 0;
  150.             }
  151.         }
  152.         if(fTakeMax) {
  153.             m_Levels[lKeyPos] = max(dwKeyValue,m_Levels[lKeyPos]);
  154.         }
  155.         else {
  156.             if((m_Levels[lKeyPos] & LOG_FORCIBLY_SET) == 0) {
  157.                 m_Levels[lKeyPos] = dwKeyValue;
  158.             }
  159.         }
  160.     }
  161.  
  162.     /*  Read the timeout value for catching hangs */
  163.     dwKeySize = sizeof(DWORD);
  164.     lReturn = RegQueryValueEx(hKey,                       // Handle to an open key
  165.         TimeoutName,                // Subkey name derivation
  166.         NULL,                       // Reserved field
  167.         &dwKeyType,                 // Returns the field type
  168.         (LPBYTE) &dwWaitTimeout,    // Returns the field's value
  169.         &dwKeySize);               // Number of bytes transferred
  170.  
  171.     /* If either the key was not available or it was not a DWORD value
  172.     then we ensure only the high priority debug logging is output
  173.     but we try and update the field to a zero filled DWORD value */
  174.  
  175.     if(lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD) {
  176.  
  177.         dwWaitTimeout = INFINITE;
  178.         lReturn = RegSetValueEx(hKey,                   // Handle of an open key
  179.             TimeoutName,            // Address of subkey name
  180.             (DWORD) 0,              // Reserved field
  181.             REG_DWORD,              // Type of the key field
  182.             (PBYTE) &dwWaitTimeout, // Value for the field
  183.             sizeof(DWORD));         // Size of the field buffer
  184.  
  185.         if(lReturn != ERROR_SUCCESS) {
  186.             DbgLog((LOG_ERROR,0,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos]));
  187.             dwWaitTimeout = INFINITE;
  188.         }
  189.     }
  190. }
  191.  
  192. void WINAPI DbgOutString(LPCTSTR psz) {
  193.     if(m_hOutput != INVALID_HANDLE_VALUE) {
  194.         UINT  cb = lstrlen(psz);
  195.         DWORD dw;
  196. #ifdef UNICODE
  197.         CHAR szDest[2048];
  198.         WideCharToMultiByte(CP_ACP, 0, psz, -1, szDest, NUMELMS(szDest), 0, 0);
  199.         WriteFile(m_hOutput, szDest, cb, &dw, NULL);
  200. #else
  201.         WriteFile(m_hOutput, psz, cb, &dw, NULL);
  202. #endif
  203.     }
  204.     else {
  205.         OutputDebugString(psz);
  206.     }
  207. }
  208.  
  209. /* Called by DbgInitGlobalSettings to setup alternate logging destinations
  210.  */
  211.  
  212. void WINAPI DbgInitLogTo(
  213.     HKEY hKey) {
  214.     LONG  lReturn;
  215.     DWORD dwKeyType;
  216.     DWORD dwKeySize;
  217.     TCHAR szFile[MAX_PATH] = {0};
  218.     static const TCHAR cszKey[] = TEXT("LogToFile");
  219.  
  220.     dwKeySize = MAX_PATH;
  221.     lReturn = RegQueryValueEx(hKey,                       // Handle to an open key
  222.         cszKey,                     // Subkey name derivation
  223.         NULL,                       // Reserved field
  224.         &dwKeyType,                 // Returns the field type
  225.         (LPBYTE) szFile,            // Returns the field's value
  226.         &dwKeySize);                // Number of bytes transferred
  227.  
  228.     // create an empty key if it does not already exist
  229.     //
  230.     if(lReturn != ERROR_SUCCESS || dwKeyType != REG_SZ) {
  231.         dwKeySize = sizeof(TCHAR);
  232.         lReturn = RegSetValueEx(hKey,                   // Handle of an open key
  233.             cszKey,                 // Address of subkey name
  234.             (DWORD) 0,              // Reserved field
  235.             REG_SZ,                 // Type of the key field
  236.             (PBYTE)szFile,          // Value for the field
  237.             dwKeySize);            // Size of the field buffer
  238.     }
  239.  
  240.     // if an output-to was specified.  try to open it.
  241.     //
  242.     if(m_hOutput != INVALID_HANDLE_VALUE) {
  243.         EXECUTE_ASSERT(CloseHandle(m_hOutput));
  244.         m_hOutput = INVALID_HANDLE_VALUE;
  245.     }
  246.     if(szFile[0] != 0) {
  247.         if(!lstrcmpi(szFile, TEXT("Console"))) {
  248.             m_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  249.             if(m_hOutput == INVALID_HANDLE_VALUE) {
  250.                 AllocConsole();
  251.                 m_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  252.             }
  253.             SetConsoleTitle(TEXT("ActiveX Debug Output"));
  254.         }
  255.         else if(szFile[0] &&
  256.             lstrcmpi(szFile, TEXT("Debug")) &&
  257.             lstrcmpi(szFile, TEXT("Debugger")) &&
  258.             lstrcmpi(szFile, TEXT("Deb"))) {
  259.             m_hOutput = CreateFile(szFile, GENERIC_WRITE,
  260.                 FILE_SHARE_READ,
  261.                 NULL, OPEN_ALWAYS,
  262.                 FILE_ATTRIBUTE_NORMAL,
  263.                 NULL);
  264.             if(INVALID_HANDLE_VALUE != m_hOutput) {
  265.                 static const TCHAR cszBar[] = TEXT("\r\n\r\n=====DbgInitialize()=====\r\n\r\n");
  266.                 SetFilePointer(m_hOutput, 0, NULL, FILE_END);
  267.                 DbgOutString(cszBar);
  268.             }
  269.         }
  270.     }
  271. }
  272.  
  273.  
  274.  
  275. /* This is called by DbgInitLogLevels to read the global debug settings for
  276.    each logging category for this module from the registry. Normally each
  277.    module has it's own values set for it's different debug categories but
  278.    setting the global SOFTWARE\Debug\Global applies them to ALL modules */
  279.  
  280. void WINAPI DbgInitGlobalSettings(bool fTakeMax) {
  281.     LONG lReturn;               // Create key return value
  282.     TCHAR szInfo[iDEBUGINFO];   // Constructs key names
  283.     HKEY hGlobalKey;            // Global override key
  284.  
  285.     /* Construct the global base key name */
  286.     wsprintf(szInfo,TEXT("%s\\%s"),pBaseKey,pGlobalKey);
  287.  
  288.     /* Create or open the key for this module */
  289.     lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE,   // Handle of an open key
  290.         szInfo,               // Address of subkey name
  291.         (DWORD) 0,            // Reserved value
  292.         NULL,                 // Address of class name
  293.         (DWORD) 0,            // Special options flags
  294.         KEY_ALL_ACCESS,       // Desired security access
  295.         NULL,                 // Key security descriptor
  296.         &hGlobalKey,          // Opened handle buffer
  297.         NULL);                // What really happened
  298.  
  299.     if(lReturn != ERROR_SUCCESS) {
  300.         DbgLog((LOG_ERROR,0,TEXT("Could not access GLOBAL module key")));
  301.         return;
  302.     }
  303.  
  304.     DbgInitKeyLevels(hGlobalKey, fTakeMax);
  305.     RegCloseKey(hGlobalKey);
  306. }
  307.  
  308.  
  309. /* This sets the debugging log levels for the different categories. We start
  310.    by opening (or creating if not already available) the SOFTWARE\Debug key
  311.    that all these settings live under. We then look at the global values
  312.    set under SOFTWARE\Debug\Global which apply on top of the individual
  313.    module settings. We then load the individual module registry settings */
  314.  
  315. void WINAPI DbgInitModuleSettings(bool fTakeMax) {
  316.     LONG lReturn;               // Create key return value
  317.     TCHAR szInfo[iDEBUGINFO];   // Constructs key names
  318.     HKEY hModuleKey;            // Module key handle
  319.  
  320.     /* Construct the base key name */
  321.     wsprintf(szInfo,TEXT("%s\\%s"),pBaseKey,m_ModuleName);
  322.  
  323.     /* Create or open the key for this module */
  324.     lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE,   // Handle of an open key
  325.         szInfo,               // Address of subkey name
  326.         (DWORD) 0,            // Reserved value
  327.         NULL,                 // Address of class name
  328.         (DWORD) 0,            // Special options flags
  329.         KEY_ALL_ACCESS,       // Desired security access
  330.         NULL,                 // Key security descriptor
  331.         &hModuleKey,          // Opened handle buffer
  332.         NULL);                // What really happened
  333.  
  334.     if(lReturn != ERROR_SUCCESS) {
  335.         DbgLog((LOG_ERROR,0,TEXT("Could not access module key")));
  336.         return;
  337.     }
  338.  
  339.     DbgInitLogTo(hModuleKey);
  340.     DbgInitKeyLevels(hModuleKey, fTakeMax);
  341.     RegCloseKey(hModuleKey);
  342. }
  343.  
  344.  
  345. /* Initialise the module file name */
  346.  
  347. void WINAPI DbgInitModuleName() {
  348.     TCHAR FullName[iDEBUGINFO];     // Load the full path and module name
  349.     TCHAR *pName;                   // Searches from the end for a backslash
  350.  
  351.     GetModuleFileName(m_hInst,FullName,iDEBUGINFO);
  352.     pName = _tcsrchr(FullName,'\\');
  353.     if(pName == NULL) {
  354.         pName = FullName;
  355.     }
  356.     else {
  357.         pName++;
  358.     }
  359.     lstrcpy(m_ModuleName,pName);
  360. }
  361.  
  362. struct MsgBoxMsg {
  363.     HWND hwnd;
  364.     TCHAR *szTitle;
  365.     TCHAR *szMessage;
  366.     DWORD dwFlags;
  367.     INT iResult;
  368. };
  369.  
  370. //
  371. // create a thread to call MessageBox(). calling MessageBox() on
  372. // random threads at bad times can confuse the host (eg IE).
  373. //
  374. DWORD WINAPI MsgBoxThread(
  375.   LPVOID lpParameter   // thread data
  376.   )
  377. {
  378.     MsgBoxMsg *pmsg = (MsgBoxMsg *)lpParameter;
  379.     pmsg->iResult = MessageBox(pmsg->hwnd,
  380.         pmsg->szTitle,
  381.         pmsg->szMessage,
  382.         pmsg->dwFlags);
  383.  
  384.     return 0;
  385. }
  386.  
  387. INT MessageBoxOtherThread(
  388.     HWND hwnd,
  389.     TCHAR *szTitle,
  390.     TCHAR *szMessage,
  391.     DWORD dwFlags) {
  392.     if(g_fDbgInDllEntryPoint) {
  393.         // can't wait on another thread because we have the loader
  394.         // lock held in the dll entry point.
  395.         return MessageBox(hwnd, szTitle, szMessage, dwFlags);
  396.     }
  397.     else {
  398.         MsgBoxMsg msg = {hwnd, szTitle, szMessage, dwFlags, 0};
  399.         DWORD dwid;
  400.         HANDLE hThread = CreateThread(0,                      // security
  401.             0,                      // stack size
  402.             MsgBoxThread,
  403.             (void *)&msg,           // arg
  404.             0,                      // flags
  405.             &dwid);
  406.         if(hThread) {
  407.             WaitForSingleObject(hThread, INFINITE);
  408.             CloseHandle(hThread);
  409.             return msg.iResult;
  410.         }
  411.  
  412.         // break into debugger on failure.
  413.         return IDCANCEL;
  414.     }
  415. }
  416.  
  417. /* Displays a message box if the condition evaluated to FALSE */
  418.  
  419. void WINAPI DbgAssert(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine) {
  420.     if(g_fUseKASSERT) {
  421.         DbgKernelAssert(pCondition, pFileName, iLine);
  422.     }
  423.     else {
  424.  
  425.         TCHAR szInfo[iDEBUGINFO];
  426.  
  427.         wsprintf(szInfo, TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"),
  428.             pCondition, iLine, pFileName);
  429.  
  430.         INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("ASSERT Failed"),
  431.             MB_SYSTEMMODAL |
  432.             MB_ICONHAND |
  433.             MB_YESNOCANCEL |
  434.             MB_SETFOREGROUND);
  435.  
  436.         switch(MsgId) {
  437.             case IDNO:              /* Kill the application */
  438.                 FatalAppExit(FALSE, TEXT("Application terminated"));
  439.                 break;
  440.  
  441.             case IDCANCEL:          /* Break into the debugger */
  442.                 DebugBreak();
  443.                 break;
  444.  
  445.             case IDYES:             /* Ignore assertion continue execution */
  446.                 break;
  447.         }
  448.     }
  449. }
  450.  
  451. /* Displays a message box at a break point */
  452.  
  453. void WINAPI DbgBreakPoint(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine) {
  454.     if(g_fUseKASSERT) {
  455.         DbgKernelAssert(pCondition, pFileName, iLine);
  456.     }
  457.     else {
  458.         TCHAR szInfo[iDEBUGINFO];
  459.  
  460.         wsprintf(szInfo, TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"),
  461.             pCondition, iLine, pFileName);
  462.  
  463.         INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("Hard coded break point"),
  464.             MB_SYSTEMMODAL |
  465.             MB_ICONHAND |
  466.             MB_YESNOCANCEL |
  467.             MB_SETFOREGROUND);
  468.  
  469.         switch(MsgId) {
  470.             case IDNO:              /* Kill the application */
  471.                 FatalAppExit(FALSE, TEXT("Application terminated"));
  472.                 break;
  473.  
  474.             case IDCANCEL:          /* Break into the debugger */
  475.                 DebugBreak();
  476.                 break;
  477.  
  478.             case IDYES:             /* Ignore break point continue execution */
  479.                 break;
  480.         }
  481.     }
  482. }
  483.  
  484. void WINAPI DbgBreakPoint(const TCHAR *pFileName,INT iLine,const TCHAR* szFormatString,...) {
  485.     // A debug break point message can have at most 2000 characters if 
  486.     // ANSI or UNICODE characters are being used.  A debug break point message
  487.     // can have between 1000 and 2000 double byte characters in it.  If a 
  488.     // particular message needs more characters, then the value of this constant
  489.     // should be increased.
  490.     const DWORD MAX_BREAK_POINT_MESSAGE_SIZE = 2000;
  491.  
  492.     TCHAR szBreakPointMessage[MAX_BREAK_POINT_MESSAGE_SIZE];
  493.  
  494.     const DWORD MAX_CHARS_IN_BREAK_POINT_MESSAGE = sizeof(szBreakPointMessage) / sizeof(TCHAR);
  495.  
  496.     va_list va;
  497.     va_start(va, szFormatString);
  498.  
  499.     int nReturnValue = _vsntprintf(szBreakPointMessage, MAX_CHARS_IN_BREAK_POINT_MESSAGE, szFormatString, va);
  500.  
  501.     va_end(va);
  502.  
  503.     // _vsnprintf() returns -1 if an error occurs.
  504.     if(-1 == nReturnValue) {
  505.         DbgBreak("ERROR in DbgBreakPoint().  The variable length debug message could not be displayed because _vsnprintf() failed.");
  506.         return;
  507.     }
  508.  
  509.     ::DbgBreakPoint(szBreakPointMessage, pFileName, iLine);
  510. }
  511.  
  512.  
  513. /* When we initialised the library we stored in the m_Levels array the current
  514.    debug output level for this module for each of the five categories. When
  515.    some debug logging is sent to us it can be sent with a combination of the
  516.    categories (if it is applicable to many for example) in which case we map
  517.    the type's categories into their current debug levels and see if any of
  518.    them can be accepted. The function looks at each bit position in turn from
  519.    the input type field and then compares it's debug level with the modules.
  520.  
  521.    A level of 0 means that output is always sent to the debugger.  This is
  522.    due to producing output if the input level is <= m_Levels.
  523. */
  524.  
  525.  
  526. BOOL WINAPI DbgCheckModuleLevel(DWORD Type,DWORD Level) {
  527.     if(g_fAutoRefreshLevels) {
  528.         // re-read the registry every second. We cannot use RegNotify() to
  529.         // notice registry changes because it's not available on win9x.
  530.         static g_dwLastRefresh = 0;
  531.         DWORD dwTime = timeGetTime();
  532.         if(dwTime - g_dwLastRefresh > 1000) {
  533.             g_dwLastRefresh = dwTime;
  534.  
  535.             // there's a race condition: multiple threads could update the
  536.             // values. plus read and write not synchronized. no harm
  537.             // though.
  538.             DbgInitModuleSettings(false);
  539.         }
  540.     }
  541.  
  542.  
  543.     DWORD Mask = 0x01;
  544.  
  545.     // If no valid bits are set return FALSE
  546.     if((Type & ((1<<iMAXLEVELS)-1))) {
  547.  
  548.         // speed up unconditional output.
  549.         if(0==Level)
  550.             return(TRUE);
  551.  
  552.         for(LONG lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  553.             if(Type & Mask) {
  554.                 if(Level <= (m_Levels[lKeyPos] & ~LOG_FORCIBLY_SET)) {
  555.                     return TRUE;
  556.                 }
  557.             }
  558.             Mask <<= 1;
  559.         }
  560.     }
  561.     return FALSE;
  562. }
  563.  
  564.  
  565. /* Set debug levels to a given value */
  566.  
  567. void WINAPI DbgSetModuleLevel(DWORD Type, DWORD Level) {
  568.     DWORD Mask = 0x01;
  569.  
  570.     for(LONG lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  571.         if(Type & Mask) {
  572.             m_Levels[lKeyPos] = Level | LOG_FORCIBLY_SET;
  573.         }
  574.         Mask <<= 1;
  575.     }
  576. }
  577.  
  578. /* whether to check registry values periodically. this isn't turned
  579.    automatically because of the potential performance hit. */
  580. void WINAPI DbgSetAutoRefreshLevels(bool fAuto) {
  581.     g_fAutoRefreshLevels = fAuto;
  582. }
  583.  
  584. #ifdef UNICODE
  585. // 
  586. // warning -- this function is implemented twice for ansi applications
  587. // linking to the unicode library
  588. // 
  589. void WINAPI DbgLogInfo(DWORD Type,DWORD Level,const CHAR *pFormat,...) {
  590.     /* Check the current level for this type combination */
  591.  
  592.     BOOL bAccept = DbgCheckModuleLevel(Type,Level);
  593.     if(bAccept == FALSE) {
  594.         return;
  595.     }
  596.  
  597.     TCHAR szInfo[2000];
  598.  
  599.     /* Format the variable length parameter list */
  600.  
  601.     va_list va;
  602.     va_start(va, pFormat);
  603.  
  604.     lstrcpy(szInfo,m_ModuleName);
  605.     wsprintf(szInfo + lstrlen(szInfo),
  606.         TEXT("(tid %x) %8d : "),
  607.         GetCurrentThreadId(), timeGetTime() - dwTimeOffset);
  608.  
  609.     CHAR szInfoA[2000];
  610.     WideCharToMultiByte(CP_ACP, 0, szInfo, -1, szInfoA, NUMELMS(szInfoA), 0, 0);
  611.  
  612.     wvsprintfA(szInfoA + lstrlenA(szInfoA), pFormat, va);
  613.     lstrcatA(szInfoA, "\r\n");
  614.  
  615.     WCHAR wszOutString[2000];
  616.     MultiByteToWideChar(CP_ACP, 0, szInfoA, -1, wszOutString, NUMELMS(wszOutString));
  617.     DbgOutString(wszOutString);
  618.  
  619.     va_end(va);
  620. }
  621.  
  622. void WINAPI DbgAssert(const CHAR *pCondition,const CHAR *pFileName,INT iLine) {
  623.     if(g_fUseKASSERT) {
  624.         DbgKernelAssert(pCondition, pFileName, iLine);
  625.     }
  626.     else {
  627.  
  628.         TCHAR szInfo[iDEBUGINFO];
  629.  
  630.         wsprintf(szInfo, TEXT("%hs \nAt line %d of %hs\nContinue? (Cancel to debug)"),
  631.             pCondition, iLine, pFileName);
  632.  
  633.         INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("ASSERT Failed"),
  634.             MB_SYSTEMMODAL |
  635.             MB_ICONHAND |
  636.             MB_YESNOCANCEL |
  637.             MB_SETFOREGROUND);
  638.  
  639.         switch(MsgId) {
  640.             case IDNO:              /* Kill the application */
  641.                 FatalAppExit(FALSE, TEXT("Application terminated"));
  642.                 break;
  643.  
  644.             case IDCANCEL:          /* Break into the debugger */
  645.                 DebugBreak();
  646.                 break;
  647.  
  648.             case IDYES:             /* Ignore assertion continue execution */
  649.                 break;
  650.         }
  651.     }
  652. }
  653.  
  654. /* Displays a message box at a break point */
  655.  
  656. void WINAPI DbgBreakPoint(const CHAR *pCondition,const CHAR *pFileName,INT iLine) {
  657.     if(g_fUseKASSERT) {
  658.         DbgKernelAssert(pCondition, pFileName, iLine);
  659.     }
  660.     else {
  661.         TCHAR szInfo[iDEBUGINFO];
  662.  
  663.         wsprintf(szInfo, TEXT("%hs \nAt line %d of %hs\nContinue? (Cancel to debug)"),
  664.             pCondition, iLine, pFileName);
  665.  
  666.         INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("Hard coded break point"),
  667.             MB_SYSTEMMODAL |
  668.             MB_ICONHAND |
  669.             MB_YESNOCANCEL |
  670.             MB_SETFOREGROUND);
  671.  
  672.         switch(MsgId) {
  673.             case IDNO:              /* Kill the application */
  674.                 FatalAppExit(FALSE, TEXT("Application terminated"));
  675.                 break;
  676.  
  677.             case IDCANCEL:          /* Break into the debugger */
  678.                 DebugBreak();
  679.                 break;
  680.  
  681.             case IDYES:             /* Ignore break point continue execution */
  682.                 break;
  683.         }
  684.     }
  685. }
  686.  
  687. void WINAPI DbgKernelAssert(const CHAR *pCondition,const CHAR *pFileName,INT iLine) {
  688.     DbgLog((LOG_ERROR,0,TEXT("Assertion FAILED (%hs) at line %d in file %hs"),
  689.         pCondition, iLine, pFileName));
  690.     DebugBreak();
  691. }
  692.  
  693. #endif
  694.  
  695. /* Print a formatted string to the debugger prefixed with this module's name
  696.    Because the COMBASE classes are linked statically every module loaded will
  697.    have their own copy of this code. It therefore helps if the module name is
  698.    included on the output so that the offending code can be easily found */
  699.  
  700. // 
  701. // warning -- this function is implemented twice for ansi applications
  702. // linking to the unicode library
  703. // 
  704. void WINAPI DbgLogInfo(DWORD Type,DWORD Level,const TCHAR *pFormat,...) {
  705.  
  706.     /* Check the current level for this type combination */
  707.  
  708.     BOOL bAccept = DbgCheckModuleLevel(Type,Level);
  709.     if(bAccept == FALSE) {
  710.         return;
  711.     }
  712.  
  713.     TCHAR szInfo[2000];
  714.  
  715.     /* Format the variable length parameter list */
  716.  
  717.     va_list va;
  718.     va_start(va, pFormat);
  719.  
  720.     lstrcpy(szInfo,m_ModuleName);
  721.     wsprintf(szInfo + lstrlen(szInfo),
  722.         TEXT("(tid %x) %8d : "),
  723.         GetCurrentThreadId(), timeGetTime() - dwTimeOffset);
  724.  
  725.     _vstprintf(szInfo + lstrlen(szInfo), pFormat, va);
  726.     lstrcat(szInfo, TEXT("\r\n"));
  727.     DbgOutString(szInfo);
  728.  
  729.     va_end(va);
  730. }
  731.  
  732.  
  733. /* If we are executing as a pure kernel filter we cannot display message
  734.    boxes to the user, this provides an alternative which puts the error
  735.    condition on the debugger output with a suitable eye catching message */
  736.  
  737. void WINAPI DbgKernelAssert(const TCHAR *pCondition,const TCHAR *pFileName,INT iLine) {
  738.     DbgLog((LOG_ERROR,0,TEXT("Assertion FAILED (%s) at line %d in file %s"),
  739.         pCondition, iLine, pFileName));
  740.     DebugBreak();
  741. }
  742.  
  743.  
  744.  
  745. /* Each time we create an object derived from CBaseObject the constructor will
  746.    call us to register the creation of the new object. We are passed a string
  747.    description which we store away. We return a cookie that the constructor
  748.    uses to identify the object when it is destroyed later on. We update the
  749.    total number of active objects in the DLL mainly for debugging purposes */
  750.  
  751. DWORD WINAPI DbgRegisterObjectCreation(const CHAR *szObjectName,
  752.                                        const WCHAR *wszObjectName) {
  753.     /* If this fires you have a mixed DEBUG/RETAIL build */
  754.  
  755.     ASSERT(!!szObjectName ^ !!wszObjectName);
  756.  
  757.     /* Create a place holder for this object description */
  758.  
  759.     ObjectDesc *pObject = new ObjectDesc;
  760.     ASSERT(pObject);
  761.  
  762.     /* It is valid to pass a NULL object name */
  763.     if(pObject == NULL) {
  764.         return FALSE;
  765.     }
  766.  
  767.     /* Check we have been initialised - we may not be initialised when we are
  768.     being pulled in from an executable which has globally defined objects
  769.     as they are created by the C++ run time before WinMain is called */
  770.  
  771.     if(m_bInit == FALSE) {
  772.         DbgInitialise(GetModuleHandle(NULL));
  773.     }
  774.  
  775.     /* Grab the list critical section */
  776.     EnterCriticalSection(&m_CSDebug);
  777.  
  778.     /* If no name then default to UNKNOWN */
  779.     if(!szObjectName && !wszObjectName) {
  780.         szObjectName = pUnknownName;
  781.     }
  782.  
  783.     /* Put the new description at the head of the list */
  784.  
  785.     pObject->m_szName = szObjectName;
  786.     pObject->m_wszName = wszObjectName;
  787.     pObject->m_dwCookie = ++m_dwNextCookie;
  788.     pObject->m_pNext = pListHead;
  789.  
  790.     pListHead = pObject;
  791.     m_dwObjectCount++;
  792.  
  793.     DWORD ObjectCookie = pObject->m_dwCookie;
  794.     ASSERT(ObjectCookie);
  795.  
  796.     if(wszObjectName) {
  797.         DbgLog((LOG_MEMORY,2,TEXT("Object created   %d (%ls) %d Active"),
  798.             pObject->m_dwCookie, wszObjectName, m_dwObjectCount));
  799.     }
  800.     else {
  801.         DbgLog((LOG_MEMORY,2,TEXT("Object created   %d (%hs) %d Active"),
  802.             pObject->m_dwCookie, szObjectName, m_dwObjectCount));
  803.     }
  804.  
  805.     LeaveCriticalSection(&m_CSDebug);
  806.     return ObjectCookie;
  807. }
  808.  
  809.  
  810. /* This is called by the CBaseObject destructor when an object is about to be
  811.    destroyed, we are passed the cookie we returned during construction that
  812.    identifies this object. We scan the object list for a matching cookie and
  813.    remove the object if successful. We also update the active object count */
  814.  
  815. BOOL WINAPI DbgRegisterObjectDestruction(DWORD dwCookie) {
  816.     /* Grab the list critical section */
  817.     EnterCriticalSection(&m_CSDebug);
  818.  
  819.     ObjectDesc *pObject = pListHead;
  820.     ObjectDesc *pPrevious = NULL;
  821.  
  822.     /* Scan the object list looking for a cookie match */
  823.  
  824.     while(pObject) {
  825.         if(pObject->m_dwCookie == dwCookie) {
  826.             break;
  827.         }
  828.         pPrevious = pObject;
  829.         pObject = pObject->m_pNext;
  830.     }
  831.  
  832.     if(pObject == NULL) {
  833.         DbgBreak("Apparently destroying a bogus object");
  834.         LeaveCriticalSection(&m_CSDebug);
  835.         return FALSE;
  836.     }
  837.  
  838.     /* Is the object at the head of the list */
  839.  
  840.     if(pPrevious == NULL) {
  841.         pListHead = pObject->m_pNext;
  842.     }
  843.     else {
  844.         pPrevious->m_pNext = pObject->m_pNext;
  845.     }
  846.  
  847.     /* Delete the object and update the housekeeping information */
  848.  
  849.     m_dwObjectCount--;
  850.  
  851.     if(pObject->m_wszName) {
  852.         DbgLog((LOG_MEMORY,2,TEXT("Object destroyed %d (%ls) %d Active"),
  853.             pObject->m_dwCookie, pObject->m_wszName, m_dwObjectCount));
  854.     }
  855.     else {
  856.         DbgLog((LOG_MEMORY,2,TEXT("Object destroyed %d (%hs) %d Active"),
  857.             pObject->m_dwCookie, pObject->m_szName, m_dwObjectCount));
  858.     }
  859.  
  860.     delete pObject;
  861.     LeaveCriticalSection(&m_CSDebug);
  862.     return TRUE;
  863. }
  864.  
  865.  
  866. /* This runs through the active object list displaying their details */
  867.  
  868. void WINAPI DbgDumpObjectRegister() {
  869.     TCHAR szInfo[iDEBUGINFO];
  870.  
  871.     /* Grab the list critical section */
  872.  
  873.     EnterCriticalSection(&m_CSDebug);
  874.     ObjectDesc *pObject = pListHead;
  875.  
  876.     /* Scan the object list displaying the name and cookie */
  877.  
  878.     DbgLog((LOG_MEMORY,2,TEXT("")));
  879.     DbgLog((LOG_MEMORY,2,TEXT("   ID             Object Description")));
  880.     DbgLog((LOG_MEMORY,2,TEXT("")));
  881.  
  882.     while(pObject) {
  883.         if(pObject->m_wszName) {
  884.             wsprintf(szInfo,TEXT("%5d (%8x) %30ls"),pObject->m_dwCookie, &pObject, pObject->m_wszName);
  885.         }
  886.         else {
  887.             wsprintf(szInfo,TEXT("%5d (%8x) %30hs"),pObject->m_dwCookie, &pObject, pObject->m_szName);
  888.         }
  889.         DbgLog((LOG_MEMORY,2,szInfo));
  890.         pObject = pObject->m_pNext;
  891.     }
  892.  
  893.     wsprintf(szInfo,TEXT("Total object count %5d"),m_dwObjectCount);
  894.     DbgLog((LOG_MEMORY,2,TEXT("")));
  895.     DbgLog((LOG_MEMORY,1,szInfo));
  896.     LeaveCriticalSection(&m_CSDebug);
  897. }
  898.  
  899. /*  Debug infinite wait stuff */
  900. DWORD WINAPI DbgWaitForSingleObject(HANDLE h) {
  901.     DWORD dwWaitResult;
  902.     do {
  903.         dwWaitResult = WaitForSingleObject(h, dwWaitTimeout);
  904.         ASSERT(dwWaitResult == WAIT_OBJECT_0);
  905.     } while(dwWaitResult == WAIT_TIMEOUT);
  906.     return dwWaitResult;
  907. }
  908. DWORD WINAPI DbgWaitForMultipleObjects(DWORD nCount,
  909.                                 CONST HANDLE *lpHandles,
  910.                                 BOOL bWaitAll) {
  911.     DWORD dwWaitResult;
  912.     do {
  913.         dwWaitResult = WaitForMultipleObjects(nCount,
  914.             lpHandles,
  915.             bWaitAll,
  916.             dwWaitTimeout);
  917.         ASSERT((DWORD)(dwWaitResult - WAIT_OBJECT_0) < MAXIMUM_WAIT_OBJECTS);
  918.     } while(dwWaitResult == WAIT_TIMEOUT);
  919.     return dwWaitResult;
  920. }
  921.  
  922. void WINAPI DbgSetWaitTimeout(DWORD dwTimeout) {
  923.     dwWaitTimeout = dwTimeout;
  924. }
  925.  
  926. #endif /* DEBUG */
  927.  
  928. #ifdef _OBJBASE_H_
  929.  
  930.     /*  Stuff for printing out our GUID names */
  931.  
  932.     GUID_STRING_ENTRY g_GuidNames[] = {
  933.     #define OUR_GUID_ENTRY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
  934.     { #name, { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } } },
  935.         #include <uuids.h>
  936.     };
  937.  
  938.     CGuidNameList GuidNames;
  939.     int g_cGuidNames = sizeof(g_GuidNames) / sizeof(g_GuidNames[0]);
  940.  
  941.     char *CGuidNameList::operator [] (const GUID &guid) {
  942.     for(int i = 0; i < g_cGuidNames; i++) {
  943.         if(g_GuidNames[i].guid == guid) {
  944.             return g_GuidNames[i].szName;
  945.         }
  946.     }
  947.     if(guid == GUID_NULL) {
  948.         return "GUID_NULL";
  949.     }
  950.  
  951.     // !!! add something to print FOURCC guids?
  952.  
  953.     // shouldn't this print the hex CLSID?
  954.     return "Unknown GUID Name";
  955. }
  956.  
  957. #endif /* _OBJBASE_H_ */
  958.  
  959. /*  CDisp class - display our data types */
  960.  
  961. // clashes with REFERENCE_TIME
  962. CDisp::CDisp(LONGLONG ll, int Format) {
  963.     // note: this could be combined with CDisp(LONGLONG) by
  964.     // introducing a default format of CDISP_REFTIME
  965.     LARGE_INTEGER li;
  966.     li.QuadPart = ll;
  967.     switch(Format) {
  968.         case CDISP_DEC: {
  969.                 TCHAR  temp[20];
  970.                 int pos=20;
  971.                 temp[--pos] = 0;
  972.                 int digit;
  973.                 // always output at least one digit
  974.                 do {
  975.                     // Get the rightmost digit - we only need the low word
  976.                     digit = li.LowPart % 10;
  977.                     li.QuadPart /= 10;
  978.                     temp[--pos] = (TCHAR) (digit+TEXT('0'));
  979.                 } while(li.QuadPart);
  980.                 wsprintf(m_String, TEXT("%s"), temp+pos);
  981.                 break;
  982.             }
  983.         case CDISP_HEX:
  984.         default:
  985.             wsprintf(m_String, TEXT("0x%X%8.8X"), li.HighPart, li.LowPart);
  986.     }
  987. };
  988.  
  989. CDisp::CDisp(REFCLSID clsid) {
  990.     WCHAR strClass[CHARS_IN_GUID+1];
  991.     StringFromGUID2(clsid, strClass, sizeof(strClass) / sizeof(strClass[0]));
  992.     ASSERT(sizeof(m_String)/sizeof(m_String[0]) >= CHARS_IN_GUID+1);
  993.     wsprintf(m_String, TEXT("%ls"), strClass);
  994. };
  995.  
  996. #ifdef __STREAMS__
  997. /*  Display stuff */
  998. CDisp::CDisp(CRefTime llTime) {
  999.     LPTSTR lpsz = m_String;
  1000.     LONGLONG llDiv;
  1001.     if(llTime < 0) {
  1002.         llTime = -llTime;
  1003.         lpsz += wsprintf(lpsz, TEXT("-"));
  1004.     }
  1005.     llDiv = (LONGLONG)24 * 3600 * 10000000;
  1006.     if(llTime >= llDiv) {
  1007.         lpsz += wsprintf(lpsz, TEXT("%d days "), (LONG)(llTime / llDiv));
  1008.         llTime = llTime % llDiv;
  1009.     }
  1010.     llDiv = (LONGLONG)3600 * 10000000;
  1011.     if(llTime >= llDiv) {
  1012.         lpsz += wsprintf(lpsz, TEXT("%d hrs "), (LONG)(llTime / llDiv));
  1013.         llTime = llTime % llDiv;
  1014.     }
  1015.     llDiv = (LONGLONG)60 * 10000000;
  1016.     if(llTime >= llDiv) {
  1017.         lpsz += wsprintf(lpsz, TEXT("%d mins "), (LONG)(llTime / llDiv));
  1018.         llTime = llTime % llDiv;
  1019.     }
  1020.     wsprintf(lpsz, TEXT("%d.%3.3d sec"),
  1021.         (LONG)llTime / 10000000,
  1022.         (LONG)((llTime % 10000000) / 10000));
  1023. };
  1024.  
  1025. #endif // __STREAMS__
  1026.  
  1027.  
  1028. /*  Display pin */
  1029. CDisp::CDisp(IPin *pPin) {
  1030.     PIN_INFO pi;
  1031.     TCHAR str[MAX_PIN_NAME];
  1032.     CLSID clsid;
  1033.  
  1034.     if(pPin) {
  1035.         pPin->QueryPinInfo(&pi);
  1036.         pi.pFilter->GetClassID(&clsid);
  1037.         QueryPinInfoReleaseFilter(pi);
  1038. #ifndef UNICODE
  1039.         WideCharToMultiByte(GetACP(), 0, pi.achName, lstrlenW(pi.achName) + 1,
  1040.             str, MAX_PIN_NAME, NULL, NULL);
  1041. #else
  1042.         lstrcpy(str, pi.achName);
  1043. #endif
  1044.     }
  1045.     else {
  1046.         lstrcpy(str, TEXT("NULL IPin"));
  1047.     }
  1048.  
  1049.     m_pString = (PTCHAR) new TCHAR[lstrlen(str)+64];
  1050.     if(!m_pString) {
  1051.         return;
  1052.     }
  1053.  
  1054.     wsprintf(m_pString, TEXT("%hs(%s)"), GuidNames[clsid], str);
  1055. }
  1056.  
  1057. /*  Display filter or pin */
  1058. CDisp::CDisp(IUnknown *pUnk) {
  1059.     IBaseFilter *pf;
  1060.     HRESULT hr = pUnk->QueryInterface(IID_IBaseFilter, (void **)&pf);
  1061.     if(SUCCEEDED(hr)) {
  1062.         FILTER_INFO fi;
  1063.         hr = pf->QueryFilterInfo(&fi);
  1064.         if(SUCCEEDED(hr)) {
  1065.             QueryFilterInfoReleaseGraph(fi);
  1066.  
  1067.             m_pString = new TCHAR[lstrlenW(fi.achName)  + 1];
  1068.             if(m_pString) {
  1069.                 wsprintf(m_pString, TEXT("%ls"), fi.achName);
  1070.             }
  1071.         }
  1072.  
  1073.         pf->Release();
  1074.  
  1075.         return;
  1076.     }
  1077.  
  1078.     IPin *pp;
  1079.     hr = pUnk->QueryInterface(IID_IPin, (void **)&pp);
  1080.     if(SUCCEEDED(hr)) {
  1081.         CDisp::CDisp(pp);
  1082.         pp->Release();
  1083.         return;
  1084.     }
  1085. }
  1086.  
  1087.  
  1088. CDisp::~CDisp() {
  1089. }
  1090.  
  1091. CDispBasic::~CDispBasic() {
  1092.     if(m_pString != m_String) {
  1093.         delete [] m_pString;
  1094.     }
  1095. }
  1096.  
  1097. CDisp::CDisp(double d) {
  1098. #ifdef DEBUG
  1099.     _stprintf(m_String, TEXT("%.16g"), d);
  1100. #else
  1101.     wsprintf(m_String, TEXT("%d.%03d"), (int) d, (int) ((d - (int) d) * 1000));
  1102. #endif
  1103. }
  1104.  
  1105.  
  1106. /* If built for debug this will display the media type details. We convert the
  1107.    major and subtypes into strings and also ask the base classes for a string
  1108.    description of the subtype, so MEDIASUBTYPE_RGB565 becomes RGB 565 16 bit
  1109.    We also display the fields in the BITMAPINFOHEADER structure, this should
  1110.    succeed as we do not accept input types unless the format is big enough */
  1111.  
  1112. #ifdef DEBUG
  1113. void WINAPI DisplayType(LPTSTR label, const AM_MEDIA_TYPE *pmtIn) {
  1114.  
  1115.     /* Dump the GUID types and a short description */
  1116.  
  1117.     DbgLog((LOG_TRACE,5,TEXT("")));
  1118.     DbgLog((LOG_TRACE,2,TEXT("%s  M type %s  S type %s"), label,
  1119.         GuidNames[pmtIn->majortype],
  1120.         GuidNames[pmtIn->subtype]));
  1121.     DbgLog((LOG_TRACE,5,TEXT("Subtype description %s"),GetSubtypeName(&pmtIn->subtype)));
  1122.  
  1123.     /* Dump the generic media types */
  1124.  
  1125.     if(pmtIn->bTemporalCompression) {
  1126.         DbgLog((LOG_TRACE,5,TEXT("Temporally compressed")));
  1127.     }
  1128.     else {
  1129.         DbgLog((LOG_TRACE,5,TEXT("Not temporally compressed")));
  1130.     }
  1131.  
  1132.     if(pmtIn->bFixedSizeSamples) {
  1133.         DbgLog((LOG_TRACE,5,TEXT("Sample size %d"),pmtIn->lSampleSize));
  1134.     }
  1135.     else {
  1136.         DbgLog((LOG_TRACE,5,TEXT("Variable size samples")));
  1137.     }
  1138.  
  1139.     if(pmtIn->formattype == FORMAT_VideoInfo) {
  1140.         /* Dump the contents of the BITMAPINFOHEADER structure */
  1141.         BITMAPINFOHEADER *pbmi = HEADER(pmtIn->pbFormat);
  1142.         VIDEOINFOHEADER *pVideoInfo = (VIDEOINFOHEADER *)pmtIn->pbFormat;
  1143.  
  1144.         DbgLog((LOG_TRACE,5,TEXT("Source rectangle (Left %d Top %d Right %d Bottom %d)"),
  1145.             pVideoInfo->rcSource.left,
  1146.             pVideoInfo->rcSource.top,
  1147.             pVideoInfo->rcSource.right,
  1148.             pVideoInfo->rcSource.bottom));
  1149.  
  1150.         DbgLog((LOG_TRACE,5,TEXT("Target rectangle (Left %d Top %d Right %d Bottom %d)"),
  1151.             pVideoInfo->rcTarget.left,
  1152.             pVideoInfo->rcTarget.top,
  1153.             pVideoInfo->rcTarget.right,
  1154.             pVideoInfo->rcTarget.bottom));
  1155.  
  1156.         DbgLog((LOG_TRACE,5,TEXT("Size of BITMAPINFO structure %d"),pbmi->biSize));
  1157.         if(pbmi->biCompression < 256) {
  1158.             DbgLog((LOG_TRACE,2,TEXT("%dx%dx%d bit  (%d)"),
  1159.                 pbmi->biWidth, pbmi->biHeight,
  1160.                 pbmi->biBitCount, pbmi->biCompression));
  1161.         }
  1162.         else {
  1163.             DbgLog((LOG_TRACE,2,TEXT("%dx%dx%d bit '%4.4hs'"),
  1164.                 pbmi->biWidth, pbmi->biHeight,
  1165.                 pbmi->biBitCount, &pbmi->biCompression));
  1166.         }
  1167.  
  1168.         DbgLog((LOG_TRACE,2,TEXT("Image size %d"),pbmi->biSizeImage));
  1169.         DbgLog((LOG_TRACE,5,TEXT("Planes %d"),pbmi->biPlanes));
  1170.         DbgLog((LOG_TRACE,5,TEXT("X Pels per metre %d"),pbmi->biXPelsPerMeter));
  1171.         DbgLog((LOG_TRACE,5,TEXT("Y Pels per metre %d"),pbmi->biYPelsPerMeter));
  1172.         DbgLog((LOG_TRACE,5,TEXT("Colours used %d"),pbmi->biClrUsed));
  1173.  
  1174.     }
  1175.     else if(pmtIn->majortype == MEDIATYPE_Audio) {
  1176.         DbgLog((LOG_TRACE,2,TEXT("     Format type %hs"),
  1177.             GuidNames[pmtIn->formattype]));
  1178.         DbgLog((LOG_TRACE,2,TEXT("     Subtype %hs"),
  1179.             GuidNames[pmtIn->subtype]));
  1180.  
  1181.         if((pmtIn->subtype != MEDIASUBTYPE_MPEG1Packet)
  1182.             && (pmtIn->cbFormat >= sizeof(PCMWAVEFORMAT))) {
  1183.             /* Dump the contents of the WAVEFORMATEX type-specific format structure */
  1184.  
  1185.             WAVEFORMATEX *pwfx = (WAVEFORMATEX *) pmtIn->pbFormat;
  1186.             DbgLog((LOG_TRACE,2,TEXT("wFormatTag %u"), pwfx->wFormatTag));
  1187.             DbgLog((LOG_TRACE,2,TEXT("nChannels %u"), pwfx->nChannels));
  1188.             DbgLog((LOG_TRACE,2,TEXT("nSamplesPerSec %lu"), pwfx->nSamplesPerSec));
  1189.             DbgLog((LOG_TRACE,2,TEXT("nAvgBytesPerSec %lu"), pwfx->nAvgBytesPerSec));
  1190.             DbgLog((LOG_TRACE,2,TEXT("nBlockAlign %u"), pwfx->nBlockAlign));
  1191.             DbgLog((LOG_TRACE,2,TEXT("wBitsPerSample %u"), pwfx->wBitsPerSample));
  1192.  
  1193.             /* PCM uses a WAVEFORMAT and does not have the extra size field */
  1194.  
  1195.             if(pmtIn->cbFormat >= sizeof(WAVEFORMATEX)) {
  1196.                 DbgLog((LOG_TRACE,2,TEXT("cbSize %u"), pwfx->cbSize));
  1197.             }
  1198.         }
  1199.         else {
  1200.         }
  1201.  
  1202.     }
  1203.     else {
  1204.         DbgLog((LOG_TRACE,2,TEXT("     Format type %hs"),
  1205.             GuidNames[pmtIn->formattype]));
  1206.         // !!!! should add code to dump wave format, others
  1207.     }
  1208. }
  1209.  
  1210.  
  1211. void WINAPI DumpGraph(IFilterGraph *pGraph, DWORD dwLevel) {
  1212.     if(!pGraph) {
  1213.         return;
  1214.     }
  1215.  
  1216.     IEnumFilters *pFilters;
  1217.  
  1218.     DbgLog((LOG_TRACE,dwLevel,TEXT("DumpGraph [%x]"), pGraph));
  1219.  
  1220.     if(FAILED(pGraph->EnumFilters(&pFilters))) {
  1221.         DbgLog((LOG_TRACE,dwLevel,TEXT("EnumFilters failed!")));
  1222.     }
  1223.  
  1224.     IBaseFilter *pFilter;
  1225.     ULONG   n;
  1226.     while(pFilters->Next(1, &pFilter, &n) == S_OK) {
  1227.         FILTER_INFO info;
  1228.  
  1229.         if(FAILED(pFilter->QueryFilterInfo(&info))) {
  1230.             DbgLog((LOG_TRACE,dwLevel,TEXT("    Filter [%x]  -- failed QueryFilterInfo"), pFilter));
  1231.         }
  1232.         else {
  1233.             QueryFilterInfoReleaseGraph(info);
  1234.  
  1235.             // !!! should QueryVendorInfo here!
  1236.  
  1237.             DbgLog((LOG_TRACE,dwLevel,TEXT("    Filter [%x]  '%ls'"), pFilter, info.achName));
  1238.  
  1239.             IEnumPins *pins;
  1240.  
  1241.             if(FAILED(pFilter->EnumPins(&pins))) {
  1242.                 DbgLog((LOG_TRACE,dwLevel,TEXT("EnumPins failed!")));
  1243.             }
  1244.             else {
  1245.  
  1246.                 IPin *pPin;
  1247.                 while(pins->Next(1, &pPin, &n) == S_OK) {
  1248.                     PIN_INFO    info;
  1249.  
  1250.                     if(FAILED(pPin->QueryPinInfo(&info))) {
  1251.                         DbgLog((LOG_TRACE,dwLevel,TEXT("          Pin [%x]  -- failed QueryPinInfo"), pPin));
  1252.                     }
  1253.                     else {
  1254.                         QueryPinInfoReleaseFilter(info);
  1255.  
  1256.                         IPin *pPinConnected = NULL;
  1257.  
  1258.                         HRESULT hr = pPin->ConnectedTo(&pPinConnected);
  1259.  
  1260.                         if(pPinConnected) {
  1261.                             DbgLog((LOG_TRACE,dwLevel,TEXT("          Pin [%x]  '%ls' [%sput]")
  1262.                                 TEXT("  Connected to pin [%x]"),
  1263.                                 pPin, info.achName,
  1264.                                 info.dir == PINDIR_INPUT ? TEXT("In") : TEXT("Out"),
  1265.                             pPinConnected));
  1266.  
  1267.                             pPinConnected->Release();
  1268.  
  1269.                             // perhaps we should really dump the type both ways as a sanity
  1270.                             // check?
  1271.                             if(info.dir == PINDIR_OUTPUT) {
  1272.                                 AM_MEDIA_TYPE mt;
  1273.  
  1274.                                 hr = pPin->ConnectionMediaType(&mt);
  1275.  
  1276.                                 if(SUCCEEDED(hr)) {
  1277.                                     DisplayType(TEXT("Connection type"), &mt);
  1278.  
  1279.                                     FreeMediaType(mt);
  1280.                                 }
  1281.                             }
  1282.                         }
  1283.                         else {
  1284.                             DbgLog((LOG_TRACE,dwLevel,
  1285.                                 TEXT("          Pin [%x]  '%ls' [%sput]"),
  1286.                                 pPin, info.achName,
  1287.                                 info.dir == PINDIR_INPUT ? TEXT("In") : TEXT("Out")));
  1288.  
  1289.                         }
  1290.                     }
  1291.  
  1292.                     pPin->Release();
  1293.  
  1294.                 }
  1295.  
  1296.                 pins->Release();
  1297.             }
  1298.  
  1299.         }
  1300.  
  1301.         pFilter->Release();
  1302.     }
  1303.  
  1304.     pFilters->Release();
  1305.  
  1306. }
  1307.  
  1308. #endif
  1309.  
  1310.  
  1311.